@extends('admin.layout.base')
@section('styles')
@endsection
@section('content')
    <div class="panel-content" id="app" v-cloak>
        <div class="panel-body">
            <div class="panel-header">
                <div class="panel-title"> 订单管理 </div>
                <el-tabs v-model="tabValue" type="card" @tab-click="search()">
                </el-tabs>
            </div>
            <div class="panel-main">
                <div class="panel-btns">
                    <el-button type="success" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                    {{--<el-button type="info" icon="fa fa-trash" @click="multiDelete()" size="small"> 删除</el-button>--}}
                </div>
                <div class="panel-search">
                    <div class="panel-search-item">
                        <el-input  size="medium" v-model="params.kwd" clearable placeholder="请输入订单号" class="input-with-select">
                            <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                        </el-input>
                    </div>
                    <div class="panel-search-item">
                        <el-date-picker
                                size="medium"
                                v-model="date"
                                type="daterange"
                                @change="pickerChange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                        </el-date-picker>
                    </div>
                </div>
                <el-table v-loading="loading"
                          element-loading-text="努力加载中..."
                          element-loading-spinner="el-icon-loading"
                          ref="multipleHandle"
                          tooltip-effect="dark"
                          sortable='custom'
                          @sort-change="sortChange"
                          @selection-change="handleSelectionChange"
                          :data="listData.data" border>
                    <el-table-column align="center" prop="order_sn" label="订单号" width="120" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column align="center" prop="pay_sn" label="支付单号" width="120" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column align="center" prop="user_name" label="买家" width="120" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column align="center" prop="store_name" label="商铺" width="120" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column align="center" prop="pay_price" label="订单金额" width="110"></el-table-column>
                    <el-table-column align="center" prop="order_number" label="订单数量" width="110"></el-table-column>
                    <el-table-column align="center" prop="order_state" label="订单状态" width="110">
                        <template slot-scope="scope"> <span v-html="scope.row.order_state_text"></span> </template>
                    </el-table-column>
                    <el-table-column align="center" prop="pay_method" label="支付方式" width="110">
                        <template slot-scope="scope"> <span v-html="scope.row.pay_method_text"></span> </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button @click="detail(scope.row.id)" icon="fa fa-edit" type="primary" size="mini" plain>详情</el-button>
                            <el-button v-if="scope.row.order_state == 3 && scope.row.pay_method == 3" @click="send(scope.row)" icon="fa fa-edit" type="success" size="mini" plain>发货</el-button>
                            {{--<el-button @click="destroy(scope.row.goods_id)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>--}}
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination"  v-if="listData && listData.total">
                    <div class="pagination-total">共 <em> @{{listData.total}} </em> 条记录</div>
                    <el-pagination v-if="listData.last_page > 1"
                                   background
                                   :page-size="listData.per_page"
                                   layout="prev, pager, next, jumper"
                                   :total="listData.total"
                                   prev-text="上一页"
                                   next-text="下一页"
                                   :current-page="listData.current_page"
                                   @current-change="changePage">
                    </el-pagination>
                </div>
            </div>
        </div>
        <!-- 表单弹窗-->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" ref="form" label-width="100px">
                <el-form-item label="物流公司：" prop="shipping_code">
                    <el-input clearable  v-model="form.shipping_code" placeholder="请输入物流公司"></el-input>
                </el-form-item>
                <el-form-item label="物流单号：" prop="shipping_sn">
                    <el-input clearable  v-model="form.shipping_sn" placeholder="请输入物流单号"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="delivery()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
@endsection
@section('scripts')
    <script src="{{ asset('js/moment.min.js') }}" type="text/javascript"></script>
    <script>
        let app = new Vue({
            el: '#app',
            data: function() {
                return {
                    listData: [],//列表数据
                    tabValue: '0',//tab
                    date: null,//日期范围
                    params: {kwd:'', name:'', page:1, status:-1, start:'', end:'', sort:''},
                    loading: false,
                    multipleSelection:[],//多选项
                    dialog: {
                        title: '',
                        top: '15vh',
                        show: false,
                        width: '600px'
                    },
                }
            },
            created(){
                this.list();
            },
            methods: {
                //分页
                changePage(val) {
                    this.params.page = val;
                    this.list();
                },
                //列表
                list(){
                    let that = this;
                    this.loading = true;
                    setTimeout(()=>{
                        $.get("{{ route('admin.order.list') }}",this.params,function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.listData = res.data;
                                that.loading = false;
                            }
                        },'JSON');
                    },500);
                },
                //详情
                detail(id){
                    window.location.href = "{{ route('admin.order.detail') }}" + '?id=' + id;
                },
                //发货弹窗
                send(info){
                    this.form = {
                        id: info.id,
                        shipping_status: info.shipping_status,
                        shipping_code: info.shipping_code,
                        shipping_sn: info.shipping_sn,
                        _token: "{{csrf_token()}}"
                    };
                    this.dialog.title = '发货';
                    this.dialog.show = true;
                },
                //发货
                delivery(id){
                    this.$refs['form'].validate((valid) => {
                        if (valid) {
                            let that = this;
                            if(this.form.shipping_status == 2){
                                that.$message.error('无须重复操作！');
                                return false;
                            }
                            if(this.form.shipping_sn.length <= 0){
                                that.$message.error('请填写物流单号！');
                                return false;
                            }
                            if(this.form.shipping_code.length <= 0){
                                that.$message.error('请填写配送方式！');
                                return false;
                            }
                            $.post("{{ route('admin.order.delivery') }}",this.form,function (res) {
                                if(res.code){
                                    that.$message.error(res.msg);
                                }else{
                                    that.$message({
                                        type: 'success',
                                        duration: '1000',
                                        message: res.msg,
                                        onClose:() =>{
                                            that.dialog.show = false;
                                            that.list();
                                        }
                                    });
                                }
                            },'JSON');
                        }
                    });
                },
                //时间筛选
                pickerChange() {
                    if (this.date) {
                        this.params.start = moment(this.date[0]).format('YYYY-MM-DD');
                        this.params.end = moment(this.date[1]).format('YYYY-MM-DD');
                    } else {
                        this.params.start = '';
                        this.params.end = '';
                    }
                },
                //排序
                sortChange(column){
                    switch (column.order) {
                        case 'ascending':
                            this.params.sort = 'asc';
                            break;
                        case 'descending':
                            this.params.sort = 'desc';
                            break;
                        default:
                            break;
                    }
                    this.list();
                },
                //筛选
                search(){
                    this.params.page = 1;
                    this.list();
                },
                // 多选删除
                multiDelete(){
                    if(this.multipleSelection.length == 0){
                        this.$message.error('请至少选择一项你要删除的项');return false;
                    }
                    this.destroy(this.multipleSelection);
                },
                handleSelectionChange(val) {
                    let select = [];
                    if(val.length > 0){
                        val.filter((item)=>{
                            if(select.indexOf(item.id) === -1){
                                select.push(item.id)
                            }
                        });
                        this.multipleSelection = select;
                    }else{
                        this.multipleSelection = [];
                    }
                },
                //删除
                destroy(id){
                    let that = this;
                    this.$confirm('确定要删除选中的活动信息吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        $.post("{{ route('admin.order.destroy') }}",{id: id, _token: "{{csrf_token()}}"},function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.list();
                            }
                        },'JSON');
                    }).catch(() => {});
                },
            }
        });
    </script>
@endsection
